/**
 * Created by Freeman on 2017/3/16.
 */
import * as types from '../types'
import * as constants from '../../AppConstants'
import * as actions from '../actions/user_actions'
import utils from '../../commons/utils'

// 用户初始化信息
const DEFAULT_USER_INFO = {
  "address": "",
  "area": "",
  "avatar": "",
  "birthDay": "",
  "channel": "",
  "city": "",
  "id": -1,
  "name": "",
  "namePinYin": "",
  "nickname": "",
  "phone": "",
  "province": "",
  "sex": 1,
  "userSettings": {
    "showClass": false,
    "showPhone": false
  },
  "username": "",
  "dynamic": {
    "favoriteCount": 0,
    "draftCount": 0,
    "publishCount": 0
  },
  "identityTag": {
    "tags": [],
    "identities": {
      "isParent": false,
      "isTeacher": false
    }
  },
  "classList": []
}
// initial state
const state = {
  userInfo: DEFAULT_USER_INFO,    // 当前登录用户的用户信息
  otherUserInfo: {},              // 其它用户的信息（当前登录用户查看其它用户的信息）
  verifyCodeCountDown: 0,         // 短信验证码倒计时
}


const mutations = {
  [types.USER_LOGOUT_SUCCESS] (state) {
    state.userInfo = DEFAULT_USER_INFO
    state.otherUserInfo = {}
  },
  [types.UPDATE_USER_INFO](state, userInfo) {
    state.userInfo = userInfo || {};
  },
  [types.UPDATE_USER_SETTINGS]({userInfo}, {field, value, data}) {
    userInfo.userSettings[field] = value
    if (field == constants.SETTING_SHOW_PHONE) {
      userInfo.phone = data
    }
  },
  [types.UPDATE_VERIFYCODE_COUNT_DOWN](state, countDown) {
    state.verifyCodeCountDown = countDown
  },
  [types.UPDATE_OTHER_USER_INFO](state, userInfo) {
    state.otherUserInfo = userInfo
  },
}


export default {
  state,
  actions,
  mutations
}
